<template>
  <div class="user-edit">
    <!-- Drawer抽屉 -->
    <Drawer :title="title" v-model="visible" width="720" draggable :mask-closable="type=='0'">
      <div :style="{maxHeight: maxHeight}" class="drawer-content">
        <Form ref="form" :model="form" :rules="formValidate" label-position="top">
            <FormItem label="行业名称" prop="businessName">
                <Input v-model="form.businessName" />
            </FormItem>
            <FormItem label="备注" prop="description">
                <Input v-model="form.description" type="textarea" />
            </FormItem>
        </Form>
      </div>
      <div class="drawer-footer br" v-show="type!=='0'">
        <Button type="primary" :loading="submitLoading" @click="submit">提交</Button>
        <Button @click="visible = false">取消</Button>
      </div>
    </Drawer>
  </div>
</template>

<script>
import { createOrUpdateBusiness } from "@/api/index";
export default {
  name: "notice",
  props: {
    value: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object
    },
    type: {
      type: String,
      default: "0"
    }
  },
  data() {
    return {
      visible: this.value,
      title: "",
      submitLoading: false,
      maxHeight: 510,
      form: {},
      formValidate: {
        // 表单验证规则
        businessName: [{ required: true, message: "请输入行业名称", trigger: "blur" }]
      },
    };
  },
  methods: {
    init() {

    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.type === "1") {
            // 编辑
            this.submitLoading = true;
            createOrUpdateBusiness(this.form).then(res => {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.$emit("on-submit", true);
                this.visible = false;
              }
            });
          } else {
            // 添加
            delete this.form.id;
            this.submitLoading = true;
            createOrUpdateBusiness(this.form).then(res => {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.$emit("on-submit", true);
                this.visible = false;
              }
            }).catch(err => {
                console.log(err)
            })
          }
        }
      });
    },
    setCurrentValue(value) {
      if (value === this.visible) {
        return;
      }
      if (this.type === "1") {
        this.title = "编辑行业";
        this.maxHeight = "100%";
        // this.maxHeight = Number(document.documentElement.clientHeight - 121) + "px";
      } else if (this.type === "2") {
        this.title = "添加行业";
        this.maxHeight = "100%";
      } else {
        this.title = "岗位行业";
        this.maxHeight = "100%";
      }
      // 清空数据
      this.$refs.form.resetFields();
      if (this.type === "0" || this.type === "1") {
        // 回显数据
        let data = this.data;
        console.log(this.data)
        // 回显
        this.form = data;
      } else {
        this.form = {
          businessName: "",
          description: '',
        };
      }
      this.visible = value;
    }
  },
  watch: {
    value(val) {
      this.setCurrentValue(val);
    },
    visible(value) {
      this.$emit("input", value);
    }
  },
  mounted() {
  }
};
</script>

<style lang="less">
@import "../../../styles/table-common.less";
.drawer-content {
  overflow: auto;
}
.drawer-content::-webkit-scrollbar {
  display: none;
}
.user-title {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  .info-title {
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
    display: block;
    margin-right: 16px;
  }
}
.info-header {
  font-size: 16px;
  color: rgba(0, 0, 0, 0.85);
  display: block;
  margin-bottom: 16px;
}
</style>

